import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login/SignIn'
import DashBoard from '../views/dashboard/index'
import firstPage from '../views/index/index'
import Smap from '../views/map/index'
import Personal from '../views/personal/index'
import Stock from '../views/stock/index'
import Commodity from '../views/commodity/index'
import Analysis from '../views/analysis/index'
Vue.use(VueRouter);

const routes = [
  {
    path:'/login',
    component: Login
  },{
    path:'/',
    children:[{
      path:'/',
      name: '主页',
      component: firstPage
    },{
      path:'/smap',
      name: '可视化管理',
      component: Smap
    },{
      path:'/personal',
      name: '个人资料管理',
      component: Personal
    },{
      path: '/commodity',
      name: '商品管理',
      component: Commodity
    },{
      path: '/stock',
      name: '商品库存管理',
      component: Stock
    },{
      path: '/analysis',
      name: '销售记录分析',
      component: Analysis
    }],
    component: DashBoard
  }
];

const router = new VueRouter({
  // base: '/admin',
  routes,
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
  let account = window.sessionStorage.getItem('user');
  if(to.path === '/login' && account != null){
    next('/');
  } else if (to.path !== '/login' && account == null) {
    next('/login');
  }
  next();
});

export default router
